<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Letter extends Model
{
    use SoftDeletes;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
    ];

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = [
        'id',
        //'updated_at',
        'deleted_at'
    ];


    /**
     * @return string
     */
    public function getPhotoUrlAttribute() {

        $photo = LetterPhoto::where('letter_id', $this->attributes['id'])->orderBy('id', 'desc')->first();

        $qiniu_bucket_url = "http://7xohc2.com1.z0.glb.clouddn.com/";

        if (is_null($photo)) {
            $photo_url = $qiniu_bucket_url."defaultLetterImage";
        } else {
            $photo_url = $qiniu_bucket_url.$photo->uuid;
        }

        return $photo_url;

    }

    public function getSenderAttribute() {

        return User::find($this->attributes['sender_id']);

    }

    public function getReceiverAttribute() {

        return User::find($this->attributes['receiver_id']);

    }

//    public function getApplicantsAttribute() {
//        //applicant is appointment sender
//        $applicants = Appointment::select('sender_id')
//
//
//        where([
//            ['book_id', $this->attributes['id']],
//            ['receiver_id', $this->attributes['holder_id']]
//        ])
//            ->orderBy('id', 'desc')
//            ->get();
//
//        return $applicants;
//    }

    /**
     * The accessors to append to the model's array form.
     *
     * @var array
     */
    protected $appends = ['photo_url', 'sender', 'receiver'];

}
